package com.globalwave.util
{
    import com.globalwave.common.LoadingWindow;
    
    import flash.display.DisplayObject;
    
    import mx.controls.Alert;
    import mx.core.Application;
    import mx.managers.PopUpManager;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.InvokeEvent;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.http.HTTPService;

    public class HttpServiceUtil 
    {
        public static var contextPath :String = "/BIZ/APP_Page" ;//"/biz" ;
        public static var ip :String = "";//"127.0.0.1" ;
        public static var port :String = "" ;
        public static var suffix : String = "" ;
        public static var loadingWindow : LoadingWindow ;
        public static var loadingWindowCount : int = 0 ;

        public static function getService(uri:String ) : HTTPService {
            var service : HTTPService = new HTTPService() ;
            service.url = getURL(uri) ;
            
            //trace(service.url) ;
            //Alert.show(this.url) ;
            //this.resultFormat="text" ;
            
            service.addEventListener(ResultEvent.RESULT,defaultResultHandler) ;
            service.addEventListener(FaultEvent.FAULT,defaultFaultHandler) ;
            service.addEventListener(InvokeEvent.INVOKE,defaultInvokeHandler) ;
            service.method = "post" ;  
            service.resultFormat = HTTPService.RESULT_FORMAT_XML ;
            
            return service ;      
        }
        
        public static function getURL(uri:String) :String {
            var result : String ;
            uri = uri.replace("_", ".aspx?action_=");
            
            if (ip == "") { 
                result = contextPath + uri ;
            } else {
                result = "http://" + ip + ":" + port + contextPath + uri + suffix; 
            }
            
            trace(" url will be send:" + result) ;
            
            return result ;
        }
        
        public static function defaultInvokeHandler(event:InvokeEvent):void {
            showLoading();        
        }
       
        public static function defaultResultHandler(event:ResultEvent):void {   
            hideLoading();
            var result : XML = new XML(event.result) ;
            if (result.elements("message") != null) {
                var msg : String = result.message + "" ;
                
                trace ("defualt result handler message is :" + msg) ;
                
                if (msg != "") {
                    Alert.show(result.message + "", "操作结果");
                }
            }
        }
       
        public static function defaultFaultHandler(event:FaultEvent):void {         
            hideLoading();     
            Alert.show( "出错啦，请与管理员联系!" + event.fault.toString(),"出错提示" ) ;   
        }
        
        public static function showLoading():void {
            if (loadingWindow != null) {
                loadingWindowCount ++ ;
                return ;
            }
          
            var app : DisplayObject = DisplayObject(Application.application) ;
            loadingWindow = 
                    LoadingWindow(PopUpManager.createPopUp(app, LoadingWindow, true));        
        }
        
        
        public static function hideLoading():void {
            if (loadingWindow != null) {
                if (loadingWindowCount >0) {
                    loadingWindowCount -- ;
                    return ;
                }
                PopUpManager.removePopUp(loadingWindow);
                loadingWindow = null ;
            }
        }
        
        
    }
}